package org.bizming.passport.service;

import java.util.List;

import org.bizming.base.BaseException;
import org.bizming.passport.dao.PassportDao;
import org.bizming.passport.model.Passport;
import org.bizming.util.ParamValiadate;
import org.bizming.util.StringHelper;

public class PassportServiceImpl implements PassportService {

	private PassportDao passportDao;

	public boolean add(Passport passport) throws BaseException {

		validateUsername(passport.getUsername());
		validatePwd(passport.getPassword());
		validateEmail(passport.getEmail());

		passport.setActionTime(System.currentTimeMillis() / 1000);
		passport.setCreateTime(System.currentTimeMillis() / 1000);

		return passportDao.add(passport);
	}

	public boolean auth(String username, String pwd) throws BaseException {
		validateUsername(username);
		validatePwd(pwd);

		Passport passport = passportDao.getByUsername(username);
		if (null == passport) {

		} else {
			if (pwd.equals(passport.getPassword())) {

			} else {

			}
		}
		return true;
	}

	public Passport getByUsername(String username) throws BaseException {
		validateUsername(username);

		return passportDao.getByUsername(username);
	}

	public Passport getByEmail(String email) throws BaseException {
		validateEmail(email);

		return passportDao.getByEmail(email);
	}

	public boolean updatePwd(String username, String pwd) throws BaseException {
		validatePwd(pwd);
		return passportDao.updatePwd(username, pwd);
	}

	public boolean updateEmail(String username, String email)
			throws BaseException {
		validateEmail(email);
		return passportDao.updateEmail(username, email);
	}

	public boolean removeByUsername(String username) throws BaseException {
		return passportDao.removeByUsername(username);
	}

	public List<Passport> selectList(int page, int pages) {
		return passportDao.selectList((page - 1) * page, pages);
	}

	/** 验证用户名 */
	private void validateUsername(String username) throws BaseException {
		if (!StringHelper.isLengthIn(username, 6, 40)) {

		}
	}

	/** 验证密码 */
	private void validatePwd(String pwd) throws BaseException {
		if (!StringHelper.isLengthIn(pwd, 6, 20)) {

		}
	}

	/** 验证email */
	private void validateEmail(String email) throws BaseException {
		if (StringHelper.isLengthIn(email, 6, 40)
				&& ParamValiadate.isEmail(email)) {
			// TODO 跳过
		} else {

		}
	}

	// =================================
	public PassportDao getPassportDao() {
		return passportDao;
	}

	public void setPassportDao(PassportDao passportDao) {
		this.passportDao = passportDao;
	}

}
